import {h, render} from 'vue'
import ConfirmComponent from './index.vue'

export default confirm = (title, content, cancelText, confirmText) => {
  return new Promise((resolve, reject) => {
      if(title && !content) {
          content = title
          title = ''
      }

      const concelHandler = () => {
        reject(new Error('取消了点击'))
      }
      const confirmHandler = () => {
        resolve()
      }

      // 关闭的回调，销毁渲染的vnode
      const close = () => {
        render(null, document.body)
      }

      const vnode = h(ConfirmComponent, {
        title,
        content,
        cancelText,
        confirmText,
        concelHandler,
        confirmHandler,
        close
      })

      render(vnode, document.body)
  })
}